// 1.1登录组件
<template>
  <!-- 2.3登录框 -->
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/1.png" alt="" />
      </div>
      <!-- 3.1导入element组件 -->
      <el-form ref="loginRuleForm" :model="loginForm" :rules="loginRules" label-width="0px" class="loginForm">
        <!-- 3.1.1用户名 -->
        <el-form-item prop="username">
          <el-input  v-model="loginForm.username" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <!-- 3.1.2密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" type="password"></el-input>
        </el-form-item>
        <!-- 3.1.3按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetForm('loginRuleForm')" >重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      // 登录表单数据
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '用户名在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '请输入正确密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm (formName) {
      // console.log(this)
      this.$refs[formName].resetFields()
      // this.$refs.loginRuleForm.resetFields()
    },
    login () {
      this.$refs.loginRuleForm.validate(async valid => {
        // console.log(valid)
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('登陆失败！')
        this.$message.success('登陆成功！')
        // 将token保存在本地，登录后删除token数据所以用sessionStorage
        sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scope>
.login_container {
  background-color: #2b4b6b;
  height: 100%;

  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    border-radius: 3px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

    .avatar_box {
      height: 130px;
      width: 130px;
      border-radius: 50%;
      border: 1px solid #eee;
      background-color: #fff;
      padding: 10px;
      box-shadow: 0 0 10px #c0c0c0;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
        border: 1px solid #c0c0c0;
      }
    }
    .loginForm {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 20px;
      box-sizing: border-box;

      .btns {
        text-align: center;
      }
    }
  }
}
</style>
